<template>
  <!-- gutter 设置间距 -->
  <el-row class="home" :gutter="50">
    <!-- :span 设置宽度  一行最多24列 -->
    <el-col :span="8" class="left">
      <el-card shadow="hover">
        <div class="user">
          <div class="avatar">
            <!-- 头像 -->
            <el-avatar
              shape="square"
              :size="120"
              fit="cover"
              :src="Uistore.getAvatarUrl('avatar')" />
          </div>
          <!-- 个人信息展示 -->
          <div class="user-info">
            <h3 class="username">admin</h3>
            <p class="user-role">超级管理员</p>
            <div class="warning-tip">
              <el-icon><Warning /></el-icon>
              <span>提示：由于获取城市和IP使用的是第三方接口，可能不稳定</span>
            </div>
          </div>
        </div>

        <!-- 登录信息展示 -->
        <div class="login-info">
          <div class="info-item">
            <span class="login-time-span">登录时间：</span>
            <p class="login-time-p">{{ Uistore.getLoginTime }}</p>
          </div>
          <div class="info-item">
            <span class="login-place-span">登录地点：</span>
            <p class="login-place-p">{{ city || '未知' }}</p>
          </div>
        </div>
      </el-card>

      <el-card shadow="hover" class="table">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            fixed
            prop="brands"
            label="系部"
            width="100px" />
          <el-table-column prop="todayPurchases" label="总人数" width="100px" />
          <el-table-column prop="monthPurchases" label="教师数" width="100px" />
          <el-table-column prop="totalPurchases" label="今日签到数" width="100px" />
        </el-table>
      </el-card>
    </el-col>

    <el-col :span="16" class="right">
      <el-card shadow="hover" class="experienceInfo">
        <el-row>
          <el-col
            v-for="item in experienceInfo"
            :key="item.icon"
            :span="8"
            class="statistic">
            <el-statistic
              :title="item.title"
              :value="item.value"
              :value-style="{ color: '#3f8600' }"
              style="margin-right: 50px">
              <el-icon>
                <component :is="item.icon" />
              </el-icon>
            </el-statistic>
          </el-col>
        </el-row>
      </el-card>

      <el-card class="chart" shadow="hover">
        <div ref="chartContainer" class="chartContainer" />
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup>
import { defineOptions, ref, onMounted, computed } from 'vue'
defineOptions({
  name: 'HomeIndex'
})

import * as echarts from 'echarts'
import { useUIStore } from '@/stores/UI'
import { getExperienceInfoMethod, getTableDataMethod, getChartDataMethod } from '@/utils/methods'
import { ElMessage } from 'element-plus'
import { Warning } from '@element-plus/icons-vue'
import axios from 'axios'
const Uistore = useUIStore()
// import { tableData } from '@/miscellaneous/index'
const tableData = ref([])
const experienceInfo = ref([])
const chartContainer = ref(null)
const city = computed(() => Uistore.city)
const ip = ref('')
let myChart = null
const initChart = async () => {
  let option = null
  try {
    const res = await getChartDataMethod()
    option = res.data

  } catch (error){
    ElMessage.error(error.message)
    return
  }
  if (!chartContainer.value) {
    return
  }
  // 初始化图表
  myChart = echarts.init(chartContainer.value)
  myChart.setOption(option)

}

const getCity = async () => {
  // 如果已经获取过城市信息，直接返回
  if (Uistore.isCityFetched) {
    return
  }

  try {
    // 获取ip
    const res1 = await axios.get('https://cn.apihz.cn/api/ip/getapi.php?id=10007097&key=10b08d22f985d23282058bd57955910d')
    ip.value = res1.data.ip
    const res2 = await axios.get(`https://cn.apihz.cn/api/ip/chaapi.php?id=10007097&key=10b08d22f985d23282058bd57955910d&ip=${ip.value}`)
    Uistore.city = 'ip:' + ip.value + '  city: ' + res2.data.shi
    Uistore.isCityFetched = true // 标记为已获取
    ElMessage.success('获取城市成功')
  } catch (error) {
    ElMessage.error(error.message)
    return
  }
}

onMounted(() => {
  // 获取表格数据
  getTableDataMethod().then(res => {
    tableData.value = res.data
  })
  // 获取合计信息
  getExperienceInfoMethod().then(res => {
    experienceInfo.value = res.data
  })
  // 获取图表数据
  initChart()
  // 获取城市（全局只获取一次）
  getCity()
})

</script>

<style scoped lang="scss">
.home {
  height: 100%;
  background: #f5f7fa;
  padding: 20px;
}

.user {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;

  .user-info {
    margin-right: 1rem;

    .username {
      font-size: 1.5rem;
      font-weight: 600;
      color: #303133;
      margin: 0 0 8px 0;
    }

    .user-role {
      font-size: 0.8rem;
      color: #606266;
      margin: 0 0 12px 0;
      padding: 4px 12px;
      background: #f0f9ff;
      border-radius: 4px;
    }

    .warning-tip {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 12px;
      background: #fffbf0;
      border: 1px solid #fdf6ec;
      border-radius: 4px;
      font-size: 0.6rem;
      color: #e6a23c;

      .el-icon {
        color: #e6a23c;
      }
    }
  }
}

// 登录信息样式
.login-info {
  margin-top: 20px;
  padding: 15px;
  border-top: 1px solid #2f4c8f;

  .info-item {
    display: flex;
    align-items: center;
    margin-bottom: 8px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  span {
    font-size: 14px;
    color: #909399;
    margin-right: 8px;
    white-space: nowrap;
  }

  p {
    font-size: 14px;
    color: #606266;
    margin: 0;
    flex: 1;
  }

  .login-time-p {
    font-weight: 500;
    color: #6f63c2;
  }

  .login-place-p {
    color: #409eff;
  }
}

.table {
  margin-top: 1rem;
}
.statistic{
  margin-bottom: 1.6rem
}
 :deep(.el-statistic__head) {
  font-size: 1rem;
  color: #897cb6;
}
.chart{
  margin-top: 1.6rem;
}
.chartContainer{
  width: 100%;
  height: 400px;
}
.warning{
  font-size: 0.8rem;
  color: red;
}
</style>
